package com.jctrip.hotel.source.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.BeanUtil;
import com.jctrip.hotel.domain.dao.HtOrderSourceRecordDao;
import com.jctrip.hotel.domain.entity.HtOrderSourceRecord;
import com.jctrip.hotel.source.model.Result;
import com.jctrip.hotel.source.model.atour.req.CancelOrderReq;
import com.jctrip.hotel.source.model.atour.req.CreateOrderReq;
import com.jctrip.hotel.source.model.atour.req.QueryOrderReq;
import com.jctrip.hotel.source.model.atour.req.QueryOrderResp;
import com.jctrip.hotel.source.model.atour.resp.*;
import com.jctrip.hotel.source.service.atour.AtourService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Optional;

@Slf4j
@Api(tags = "atour")
@RestController
@RequestMapping("/atour")
@ApiSupport(order = 1)
public class AtourController {
    @Resource
    private AtourService atourService;

    @GetMapping(value = "/getCityList")
    @ApiOperation(value = "getCityList", notes = "获取城市信息")
    @ApiOperationSupport(order = 1)
    public Result getCityList(@RequestParam(value = "appId", required = false) Integer appId,
                              @RequestParam(value = "key", required = false) String key) {
        List<CityData> result;
        try {
            result = atourService.getCityList(appId, key);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping(value = "/getHotelList")
    @ApiOperation(value = "getHotelList", notes = "获取指定城市内，酒店基础信息")
    @ApiOperationSupport(order = 2)
    public Result getHotelList(@RequestParam(value = "appId", required = false) Integer appId,
                               @RequestParam(value = "key", required = false) String key,
                               @RequestParam(value = "cityId", required = false) Integer cityId) {
        List<HotelData> result;
        try {
            result = atourService.getHotelList(appId, key, cityId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping(value = "/getHotel")
    @ApiOperation(value = "getHotel", notes = "获取酒店详细信息")
    @ApiOperationSupport(order = 3)
    public Result getHotel(@RequestParam(value = "appId", required = false) Integer appId,
                           @RequestParam(value = "key", required = false) String key,
                           @RequestParam(value = "hotelId", required = false) Integer hotelId) {
        HotelData result;
        try {
            result = atourService.getHotel(appId, key, hotelId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping(value = "/getRoomTypeList")
    @ApiOperation(value = "getRoomTypeList", notes = "获取酒店房型信息")
    @ApiOperationSupport(order = 4)
    public Result getRoomTypeList(@RequestParam(value = "appId", required = false) Integer appId,
                                  @RequestParam(value = "key", required = false) String key,
                                  @RequestParam(value = "hotelId", required = false) Integer hotelId) {
        List<RoomTypeData> result;
        try {
            result = atourService.getRoomTypeList(appId, key, hotelId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping(value = "/roomRateList")
    @ApiOperation(value = "roomRateList", notes = "房价信息")
    @ApiOperationSupport(order = 5)
    public Result roomRateList(@RequestParam(value = "appId", required = false) Integer appId,
                               @RequestParam(value = "key", required = false) String key,
                               @RequestParam(value = "hotelId", required = false) Integer hotelId,
                               @RequestParam(value = "roomTypeId", required = false) Integer roomTypeId,
                               @RequestParam(value = "start", required = false) String start,
                               @RequestParam(value = "end", required = false) String end,
                               @RequestParam(value = "roomRateTypeId", required = false) Integer roomRateTypeId,
                               @RequestParam(value = "mebId", required = false) Integer mebId) {
        List<RoomRateData> result;
        try {
            result = atourService.getRoomRateList(appId, key, hotelId, roomTypeId, start, end, roomRateTypeId, mebId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping(value = "/roomInventoryList")
    @ApiOperation(value = "roomInventoryList", notes = "库存信息")
    @ApiOperationSupport(order = 6)
    public Result roomInventoryList(@RequestParam(value = "appId") Integer appId,
                                    @RequestParam(value = "key") String key,
                                    @RequestParam(value = "hotelId") Integer hotelId,
                                    @RequestParam(value = "mebId") Integer mebId,
                                    @RequestParam(value = "roomTypeId") Integer roomTypeId,
                                    @RequestParam(value = "start") String start,
                                    @RequestParam(value = "end") String end,
                                    @RequestParam(value = "roomRateTypeId") Integer roomRateTypeId
    ) {
        List<RoomInventoryData> result;
        try {
            result = atourService.getRoomInventoryList(appId, key, hotelId, roomTypeId, start, end, roomRateTypeId, mebId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

//    @GetMapping(value = "/postOrder")
//    @ApiOperation(value = "postOrder", notes = "下单")
//    @ApiOperationSupport(order = 7)
//    public OrderDataResult postOrder(@RequestParam(value = "appId")Integer appId,
//                                     @RequestParam(value = "key")String key,
//                                     @RequestBody CreateOrderReq createOrderReq,
//                                     @RequestParam(value = "totalRate")BigDecimal totalRate){
//        return atourService.postOrder(appId, key, createOrderReq);
//    }

    @PostMapping("/queryOrder")
    @ApiOperation(value = "queryOrder", notes = "查询订单")
    @ApiOperationSupport(order = 8)
    public Result queryOrder(@RequestParam(value = "appId", required = false) Integer appId,
                             @RequestParam(value = "key", required = false) String key,
                             @RequestBody QueryOrderReq queryOrderReq) {
        QueryOrderResp result;
        try {
            result = atourService.queryOrder(appId, key, queryOrderReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/cancelOrder")
    @ApiOperation(value = "cancelOrder", notes = "取消订单")
    @ApiOperationSupport(order = 9)
    public Result cancelOrder(@RequestParam(value = "appId", required = false) Integer appId,
                              @RequestParam(value = "key", required = false) String key,
                              @RequestBody CancelOrderReq cancelOrderReq) {
        CancelOrderResp result;
        try {
            result = atourService.cancelOrder(appId, key, cancelOrderReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    // ======================数据直连部分==================
    @PostMapping(value = "/createOrder")
    @ApiOperation(value = "createOrder", notes = "直连下单")
    public Result createOrder(@RequestParam(value = "appId") Integer appId,
                              @RequestParam(value = "key") String key,
                              @RequestBody CreateOrderReq createOrderReq) {
        CreateOrderResp result;
        try {
            result = atourService.creatOrder(appId, key, createOrderReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/queryDirectOrder")
    @ApiOperation(value = "queryDirectOrder", notes = "直连查询订单详情")
    public Result queryDirectOrder(@RequestParam(value = "sourceId") @ApiParam("供应商账户id") String sourceId,
                                   @RequestParam(value = "orderNo") @ApiParam("嘉程预定订单编号") String orderNo) {
        QueryOrderResp queryOrderResp;
        try {
            QueryOrderReq queryOrderReq = QueryOrderReq.builder()
                    .orderNo(orderNo.trim())
                    .build();
            HtOrderSourceRecordDao htOrderSourceRecordDao =
                    BeanContext.getApplicationContext().getBean(HtOrderSourceRecordDao.class);
            HtOrderSourceRecord sourceRecord = Optional.ofNullable(htOrderSourceRecordDao.selectOne(new LambdaQueryWrapper<HtOrderSourceRecord>()
                    .eq(HtOrderSourceRecord::getBookId, queryOrderReq.getOrderNo()))).orElse(new HtOrderSourceRecord());
            queryOrderReq.setAtourOrderNo(sourceRecord.getSourceOrderId());
            queryOrderResp = atourService.queryDirectOrder(sourceId, queryOrderReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(queryOrderResp);
    }
}
